package info.fastpace.utils.iterator;

import java.util.List;

public abstract class PagingIterator<E> extends ConsecutiveIterator<E> {
	private int page = 0;
	private int pageSize;
	private boolean finished = false;

	public PagingIterator() {
		this(25);
	}

	public PagingIterator(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public final boolean hasNext() {
		if (super.hasNext()) {
			return true;
		}
		if (finished) {
			return false;
		}

		List<E> list = getPage(page++, pageSize);
		
		if (list.isEmpty()) {
			finished = true;
			return false;
		}

		add(list.iterator());

		return super.hasNext();
	}

	/**
	 * Empty iterator signifies no more pages
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	protected abstract List<E> getPage(int page, int pageSize);
}
